package cn.com.main;

import cn.com.utils.crypto.CrytoUtil;
import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class CryptoTest {

    public static void main(String[] args) {
        Map<String, String> accessInfo = new HashMap<>();
        String region = "cn-east-3";
        accessInfo.put("providerUrl", "https://iam." + region + ".myhuaweicloud.com/v3");
        accessInfo.put("apiKey", "0URJLH2NUWXAVA5Z734R");
        accessInfo.put("secureToken", "mdxFNtYJCQiiA0RJChoV6VAMEzPkz0Eczke3kJyO");
        accessInfo.put("region", region);
        accessInfo.put("tenantId", "0a9b023dbe80f3502f1ec0093f35a32b");
        String jsonStr = JSONUtil.toJsonStr(accessInfo);
        // 对称加密 crypto.dll
        extracted(jsonStr, CrytoUtil::encrypt, CrytoUtil::decrypt);
        // 非对称加密
        extracted(region, CrytoUtil::asymmetricEncrypt, CrytoUtil::asymmetricDecrypt);
    }

    private static void extracted(String encryptAttrData, Function<String, String> encrypt, Function<String, String> decrypt) {
        System.out.println("原始文本：" + encryptAttrData);
        encryptAttrData = encrypt.apply(encryptAttrData);
        // 输出结果
        System.out.println("加密后：" + encryptAttrData);
        System.out.println("解密后：" + decrypt.apply(encryptAttrData));
        System.out.println("-----------------------------------\n\n");
    }
}
